<?php
class goodsAction extends Action {
    public function index(){
		$this->display();
    }

    public function details(){
        $this->display();
    }
    public function group(){
		$this->display();
    }

    public function group_detail(){
        $this->display();
    }
    public function convert_list(){
        $this->display();
    }
    public function convert_detail(){
        $this->display();
    }
    public function looking(){
        $this->display();
    }
    public function looking_detail(){
        $this->display();
    }
    public function recommend_list(){
        $this->display();
    }
    public function recommend_detail(){
        $this->display();
    }
    public function snapping_list(){
        $this->display();
    }
	public function snapping_detail(){
        $this->display();
    }
	public function whole(){
        $this->display();
    }
	public function search_list(){
        $this->display();
    }
	public function specifications_select()
	{
		$goods_id=pg('goods_id');

		$id=explode(',',pg('id'));
		$name=explode(',',pg('name'));
		$goods_attribute_value_array = array();
		foreach($id as $k=>$v)
		{
			$goods_attribute_value_array[] =
			array(
				'id' => $v,
				'name' => $name[$k]
			);
		}
		$goods_attribute=M('goods_attribute')->field('goods_attribute_value_array,goods_id,price')->where(array('goods_id'=>$goods_id,'goods_attribute_value_array'=>serialize($goods_attribute_value_array)))->find();
        $goods_id=$goods_attribute['goods_id'];
        //$goods=M('activity_goods')->where(array('goods_id'=>$goods_id))->select();
        $model=new Model();
        $goods=$model->query("select relevance_id,goods_id,price,activity_price,activity_discount,activity_goods_number,goods_attribute_value_array,activity_name from  index_activity_goods left join index_activity on index_activity.activity_id=index_activity_goods.activity_id  where index_activity_goods.goods_id=$goods_id");
        if(!$goods){
            $goods_attribute['type']='no_disc';
            echo json_encode($goods_attribute);
        }else{
            $g_arr=array();
            foreach($goods as $v){
                $g_arr[]=$v;
                if($v['goods_attribute_value_array']==$goods_attribute['goods_attribute_value_array']){

                    $g_arr['type']='disc';
                    $g_arr['raw_price']=$v['price'];
                    $g_arr['font_price']=$v['activity_price'];
                    $g_arr['num']=sizeof($goods);
                }
            }
            echo json_encode($g_arr);
        }
	}

	public function package_specifications()
	{
		$goods_id=pg('goods_id');
		
		$id=explode(',',pg('id'));
		$name=explode(',',pg('name'));
		$goods_attribute_value_array = array();
		foreach($id as $k=>$v)
		{
			$goods_attribute_value_array[] =
			array(
				'id' => $v,
				'name' => $name[$k]
			);
		}
		$goods_attribute=M('goods_attribute')->field('goods_attribute_value_array,goods_id,price')->where(array('goods_id'=>$goods_id,'goods_attribute_value_array'=>serialize($goods_attribute_value_array)))->find();
		$goods_id=$goods_attribute['goods_id'];
		echo json_encode($goods_attribute);
	}
	
	public function get_coupons()
	{
		$coupons_id=pg('coupons_id');
		$member=session('member');
		if(!empty($member))
		{
			if($coupons_id!='')
			{
				$coupons_member = M('coupons_member')->where(array('coupons_id'=>$coupons_id,'member_id'=>$member['member_id']))->find();
				if(empty($coupons_member))
				{
					$coupons=M('coupons')->where(array('coupons_id'=>$coupons_id))->find();
					M('coupons_member')->data(array(
					'type_id'=>40,
					'date'=>time(),
					'version_id'=>1,
					'coupons_id'=>$coupons_id,
					'price'=>$coupons['price'],
					'effective_date'=>$coupons['effective_date'],
					'member_id'=>$member['member_id'],
					'state'=>277
					))->add();
					echo '领取成功';
				}
				else
				{
					echo '您已经领过优惠券了';
				}
			}
		}
		else
		{
			echo '请先登录';
		}
	}


    public function fenye(){
        $fenye=session('fenye');
        ?>
        当前第<?php echo $fenye['p'] ?>/<?php echo $fenye['total'] ?>页&nbsp;&nbsp;共<?php echo $fenye['cnt'] ?>条&nbsp;&nbsp;每页8条
        <span >
		     <a onclick="changepage(1)">首页</a>
            <?php if($fenye['p']>=2){  ?>
                <a onclick="changepage(<?php echo $fenye['p']-1 ?>)">上一页</a>
            <?php } ?>
            <?php  for ($i=1; $i <=$fenye['total'] ; $i++) {
                ?>
                <a onclick="changepage(<?php echo $i ?>)"><?php echo $i ?></a>
            <?php } ?>
            <?php if($fenye['p']<$fenye['total']){  ?>
                <a onclick="changepage(<?php echo $fenye['p']+1 ?>)">下一页</a>
            <?php } ?>
            <a onclick="changepage(<?php echo $fenye['total'] ?>)">尾页</a>
                转到<select onchange="changepage(this.options[selectedIndex].value);">
                    <?php  for ($i=1; $i <=$fenye['total'] ; $i++) {?>
                        <option  <?php if($i==$fenye['p']){ echo "selected='selected'";} ?> value="<?php echo $i ?>"><?php echo $i ?></option>
                    <?php } ?>
                    </select>页
        </span>
        <?php
    }






    public function query(){
        $arr=$_POST;
        // $arr['goods_classify_id']=$_POST['classify'];

        unset($arr['classify']);
        $where=$arr;
        // 分类
        $perpage=6;$offset=($p-1)*$perpage;
        //
        $paixu=$arr['jiegdes'].' '.$arr['nsduia'];
        $newarr=M('goods')->where(array('type_id'=>3))->order($paixu)->select();
        //echo  M()->getlastsql();
        //dump($newarr);
        //$newarr=$list;
         //echo  M()->getlastsql();exit;
        //筛选出符合结果的数组
        //存入session
        session('newarr',$newarr);
        $p=pg('p');
        $perpage=8;
        $p=$p?$p:1;
        //分页数组
        $fenye['p']=$p;
        $fenye['cnt']=count($newarr);
        $fenye['total']=ceil(count($newarr)/$perpage);
        session('fenye',$fenye);
        //大于
        $offset= $perpage*($p-1);
        //小于
        $small= $perpage*($p);
        // 分页筛选
        //循环输出
        foreach ($newarr as $k => $v) {
            if($k>=$offset && $k<$small){
                ?>
                <li class="commodity_list_item">
                    <div class="commodity_list_item_shade">
                        <span class="commodity_list_item_shade_but goumai" onclick='goumai(<?php echo $v['goods_id']; ?>,"<?php echo $v['goods_name'];?>")'>购买</span>
                    </div>
                    <img src="<?php echo $v['goods_img'];?>" alt="" />
                    <p class="commodity_list_item_msg">
                        <span class="commodity_list_item_price"><?php echo $v['price'];?></span>
                        <?php if($v['is_index']==1){ ?>
                        <span class="commodity_list_item_weight">包邮</span>
                        <?php } ?>
                    </p>
                    <h6 class="commodity_list_item_title"><?php echo $v['goods_name'];?></h6>
                    <p class="commodity_list_item_number">
                        <span class="commodity_list_item_number_express"><?php echo $v['kuaidis'];?></span>
                        <span class="commodity_list_item_number_but goumai"  onclick='goumai(<?php echo $v['goods_id']; ?>,"<?php echo $v['goods_name'];?>")'>购买</span></p>
                </li>
            <?php	} ?>
        <?php	}
    }



}



